package com.shopping.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.shopping.util.DB;

public class User {
	private int id;
	private String username;
	private String password;
	private String phone;
	private String addr;
	private Date rdate;
	
	public String getAddr() {
		return addr;
	}
	
	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getPhone() {
		return phone;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public Date getRdate() {
		return rdate;
		
	}
	
	public void setRdate(Date rdate) {
		this.rdate = rdate;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public void save() {
		Connection conn = null;
		String sql = "insert into ruser values (null,?,?,?,?,?)";
		PreparedStatement prepStat = null;
		
		try {
			conn = DB.getConn();
			prepStat = DB.prepStat(conn,sql);
			prepStat.setString(1,username);
			prepStat.setString(2,password);
			prepStat.setString(3,phone);
			prepStat.setString(4,addr);
			prepStat.setTimestamp(5,new Timestamp(rdate.getTime()));
			prepStat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closePrepStat(prepStat);
			DB.closeConn(conn);
		}
	}
	
	public static List<User> getUsers() {
		List<User> list = new ArrayList<User>();
		Connection conn = null;
		ResultSet rs = null;
		
		try {
			conn = DB.getConn();
			String sql = "select * from ruser";
			rs = DB.executeQuery(conn, sql);
			while(rs.next()) {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPhone(rs.getString("Phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closeRs(rs);
			DB.closeConn(conn);
			
		}
		return list;
	}
	
	public static boolean userExists(String username) {
		//check table find out username exis or not
		return false;
	}
	
	public static boolean isPasswordCorrect(String username,String password) {
		//check table find out username exis or not
		return false;
	}
	
	public static User validate(String username,String password) throws UserNotFoundException, PasswordNotCorrectException {
		Connection conn = null;
		ResultSet rs = null;
		User u = null;
		String sql = "select * from ruser where username = '" + username + "'" ;
		try {
		  conn = DB.getConn();	
		  rs = DB.executeQuery(conn, sql);
		
		    if(!rs.next()) {
			  throw new UserNotFoundException();
		    } else if(!rs.getString("password").equals(password)) {
		    	throw new PasswordNotCorrectException();
		    } else {
		    	u = new User();
		    	u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPhone(rs.getString("Phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
		    }
	   } catch(SQLException e) {
		   e.printStackTrace();
	   } finally {
		   DB.closeRs(rs);
		   DB.closeConn(conn);
	   }
	   return u;
	}
	
	public void Update() {
		Connection conn = null;
		String sql = "Update ruser set username=?,phone=?,addr=? where id =" + this.id;
		PreparedStatement prepStat = null;
		
		try {
			conn = DB.getConn();
			prepStat = DB.prepStat(conn,sql);
			prepStat.setString(1,username);
			prepStat.setString(2,phone);
			prepStat.setString(3,addr);
			prepStat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closePrepStat(prepStat);
			DB.closeConn(conn);
		}
	}
}
